package com.csdj.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csdj.pojo.Student;
import com.csdj.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/getStudentCount")
    public ModelAndView getStudentCount(){
        ModelAndView modelAndView = new ModelAndView();
        long result = studentService.count();
        modelAndView.addObject("result",result);
        modelAndView.setViewName("student");
        return  modelAndView;
    }

    @RequestMapping("/addStudent")
    public ModelAndView addStudent(){
        ModelAndView modelAndView = new ModelAndView();
        Student student = new Student();
        student.setStudentName("高林威");
        student.setStudentSex("男");
        student.setStudentAge(20);
        boolean  result =   studentService.save(student);
        modelAndView.addObject("result",result);
        modelAndView.setViewName("student");
        return  modelAndView;
    }

    /**
     * 分页查询学生集合
     * @return 模型视图对象
     */
    @RequestMapping("/pageStudent")
    public ModelAndView pageStudent(){
        ModelAndView modelAndView = new ModelAndView();
        //创建分页对象，设置当前页和每页显示的条数
        Page<Student> page = studentService.page(new Page<>(1,3));
        //创建查询构造器对象
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        //设置条件查询
        queryWrapper.eq(Student::getStudentSex, "男");
        //调用MyBatisPlus的分页查询方法,传入分页对象，条件查询为queryWrapper
        studentService.page(page,queryWrapper);
        modelAndView.addObject("page",page);
        modelAndView.setViewName("studentList");
        return  modelAndView;
    }
}
